package cn.ekuma.epos.security.dao;

import java.util.Date;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.TableDBSentenceBuilder;
import com.openbravo.data.loader.serialize.DataParams;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteParams;
import com.openbravo.data.loader.sentence.StaticSentence;
import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

import e.odbo.data.dao.BaseDAO;
import cn.ekuma.epos.security.AppLock;
import cn.ekuma.epos.security.db.table.I_AppLock;

public class AppLockDAO extends BaseDAO<AppLock> {

    public AppLockDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return new TableDefinition(
                "APP_LOCK",
                new Field[]{
                        new Field(I_AppLock.ID, Datas.STRING, Formats.STRING),
                        new Field(I_AppLock.ASKFOR, Datas.STRING, Formats.STRING),
                        new Field(I_AppLock.TARGER, Datas.STRING, Formats.STRING),
                        new Field(I_AppLock.TARGERID, Datas.STRING, Formats.STRING),
                        new Field(I_AppLock.MEMO, Datas.STRING, Formats.STRING),
                        new Field(I_AppLock.LOCKTIME, Datas.TIMESTAMP, Formats.TIMESTAMP)
                },
                new int[]{0}
        );
    }

    @Override
    public void writeInsertValues(DataWrite dp, AppLock obj)
            throws BasicException {
        dp.setString(1, obj.getKey());
        dp.setString(2, obj.getAskFor());
        dp.setString(3, obj.getTarger());
        dp.setString(4, obj.getTargetId());
        dp.setString(5, obj.getMemo());
        dp.setTimestamp(6, obj.getLockTime());

    }

    @Override
    public Class getSuportClass() {
        return AppLock.class;
    }


    public AppLock readValues(DataRead dr, AppLock obj) throws BasicException {
        if (obj == null)
            obj = new AppLock();
        obj.setKey(dr.getString(1));
        obj.setAskFor(dr.getString(2));
        obj.setTarger(dr.getString(3));
        obj.setTargetId(dr.getString(4));
        obj.setMemo(dr.getString(5));
        obj.setLockTime(dr.getTimestamp(6));
        return obj;
    }


    public boolean lock(String askFor, String target, String targetId,
                        String memo) throws BasicException{
        AppLock obj = new AppLock();
        obj.setAskFor(askFor);
        obj.setTarger(target);
        obj.setTargetId(targetId);
        obj.setMemo(memo);
        obj.setLockTime(new Date());
        insert(obj);
        return true;
    }


    public boolean unlock(final String askFor, final String target, final String targetId) throws BasicException {
        if (tableBuilder instanceof TableDBSentenceBuilder) {
            String sql = ((TableDBSentenceBuilder) tableBuilder).getDeleteSQL(getTable(), new String[]{I_AppLock.ASKFOR,
                    I_AppLock.TARGER,
                    I_AppLock.TARGERID});
            int count=new StaticSentence(s, sql
                    , SerializerWriteParams.INSTANCE).exec(new DataParams() {
                @Override
                public void writeValues() throws BasicException {
                    setString(1, askFor);
                    setString(2, target);
                    setString(3, targetId);
                }
            });
            return count>0;
        }
        throw new BasicException("not support yet!now is db only");
    }


    public boolean unlockAllbyAskToTarget(final String askFor, final String target) throws BasicException {
        if (tableBuilder instanceof TableDBSentenceBuilder) {
            String sql = ((TableDBSentenceBuilder) tableBuilder).getDeleteSQL(getTable(), new String[]{I_AppLock.ASKFOR,
                    I_AppLock.TARGER});
            int count=new StaticSentence(s, sql
                    , SerializerWriteParams.INSTANCE).exec(new DataParams() {
                @Override
                public void writeValues() throws BasicException {
                    setString(1, askFor);
                    setString(2, target);

                }
            });
            return count>0;
        }
        throw new BasicException("not support yet!now is db only");
    }


    public boolean unlockAllbyAsk(final String askFor) throws BasicException {
        if (tableBuilder instanceof TableDBSentenceBuilder) {
            String sql = ((TableDBSentenceBuilder) tableBuilder).getDeleteSQL(getTable(), new String[]{I_AppLock.ASKFOR});
            int count=new StaticSentence(s, sql
                    , SerializerWriteParams.INSTANCE).exec(new DataParams() {
                @Override
                public void writeValues() throws BasicException {
                    setString(1, askFor);
                }
            });
            return true;
        }
        throw new BasicException("not support yet!now is db only");
    }
}
